<template>
  <div class="news">
    <!--头部选择-->
    <tmpHeader :index='10'/>
    <!--搜索/创建-->
    <div class="iheader" v-show="!flag">
      <Button type="info" @click="add">
        <i class = "iconfont icon-add" style="font-size:14px;margin-right:5px"></i>
      </Button>
      <div class="box">
        <div class="boxs">
          <Input v-model.trim="search" placeholder="请输入新闻标题查询" style="width: 200px" @on-keyup.enter="press" clearable />
        </div>
        <Button type="primary" @click="press"  icon="ios-search" style="margin-left:20px;"></Button>
      </div>
    </div>  
    <!--表格列表-->
    <div class="main">
      <Table stripe :columns="columnList" :data="tablesList"></Table>
    </div>
    <div class="page" v-show="!flag">
        <Page :total="newsSize" :current='pageNo' show-total show-elevator @on-change="pageChange"/>
    </div>
  </div>
</template>
<script>
// 动态新闻
import tmpHeader from "@/pages/organizat/contentmen/tmpHeader";
import api from "@/api/commonApi";
import axios from "axios";
export default {
  components: {
    tmpHeader
  },
  data() {
    return {
      tablesList: [],
      search: "",
      id: sessionStorage.getItem("hospitalId"),
      newsSize: 10,
      pageSize: 10,
      pageNo: 1,
      flag: false,
      columnList: [
        {
          title:"排序",
          key:"sum",
          align:"center",
          width:80,
          render: (h, params) => {
              let sum = (this.pageNo-1)*this.pageSize + params.index +1;
              return h("span", sum);
          }
        },
        {
          title:"发布终端",
          key:"type",
          align:"center",
          width:110
        },
        {
          title:"新闻通知标题",
          key:"title",
          align:"center",
        },
        {
          title:"内容来源",
          key:"source",
          align:"center",
        },       
        {
          title:"显示排序",
          key:"showindex",
          align:"center",
          width:120
        },
        {
          title:"是否发布",
          key:"enable",
          align:"center",
          width:120
        },
        {
          title:"更新时间",
          key:"updateTime",
          align:"center",
        },
        {
          title:"设置",
          align:"center",
           width:100,
          render: (h,params) => {
            let row = params.row;
            return [
              h('span',{
                style: {
                    color: " #5362e1",
                    width: "60px"
                },
                on: {
                  click :() => {
                    this.navto(row)
                  }
                }
              },'编辑')
            ]
          }
        }
      ]
    };
  },
  created(){
    let breadList = [
            { path: "/index", title: "首页" },
            {
                path: "/index/operation/mechanism/index",
                title: "机构运营"
            },
            {
                path: "/index/operation/home",
                title: "机构配置管理"
            }
        ];
        this.$emit("changeBreadList", breadList);
  },
  mounted() {
    this.status();
    let pageNo = this.$route.params.pageNo;
    if (pageNo) {
      this.pageNo = parseInt(pageNo);
    }
    this.getData(this.pageNo);
  },
  methods: {
    //分页器改变
    pageChange(index) {
      this.pageNo = index;
      if (this.search) {
        this.getData(index, this.search);
      } else {
        this.getData(index);
      }
    },
    status() {
      let flag = localStorage.getItem("status");
      if (!Boolean(flag)) {
        this.$Message.info("您还没有开通互联网医院,请先开通互联网医院");
        localStorage.setItem("homeIndex", 0);
        this.flag = true;
        setTimeout(() => {
          this.functionJS.paramsNavgationTo(this, "homeinfo", {
            // 公用方法
          }); 
        }, 1000);
      }
    },
    navto(item) {
      let id = item.id;
      this.functionJS.paramsNavgationTo(this, "newsEdit", {
        // 公用方法
        id,
        pageNo: this.pageNo
      }); 
    },
    add() {
      this.functionJS.paramsNavgationTo(this, "newsAdd", {
        // 公用方法
          pageNo: this.pageNo
      }); 
    },
    press() {
      this.pageNo = 1
      this.getData(1, this.search);
    },
    getData(pageNo, val) {
      let params = {
        hospitalId: this.id,
        pageNo,
        pageSize: 10
      };
      if (Boolean(val)) {
        params.title = val;
      }
      axios
        .post(api.newsPage, params)
        .then(res => {
          if (res.data.code) {
            let ret = res.data.object.records;
            this.newsSize = res.data.object.total;
            if(ret){
              ret.forEach((item,index) => {
                item.sum = this.addZeros(index);
                item.enable = item.enable === 1 ? '是':'否'
                // item.priority = item.priority == 1 ? '是' : '否'
                if(item.type == '0'){
                    item.type = '全部'
                } else if(item.type == '1') {
                    item.type = '院内通知'
                } else if(item.type == '2') {
                  item.type = '医生端'
                } else if(item.type == '3') {
                  item.type = '门诊出诊'
                } else if(item.type == '4') {
                  item.type = '就诊须知'
                } else if(item.type == '5') {
                  item.type = '医保指南'
                } else if(item.type == '6') {
                  item.type = '价格公示'
                } else if(item.type == '7') {
                  item.type = '热点信息'
                } else if(item.type == '8') {
                  item.type = '党风廉政'
                } else if(item.type == '9') {
                  item.type = '职权考核'
                } else if(item.type == '10') {
                  item.type = '科室职责'
                }
                item.newsHeadlines = this.analysisImages(item.newsHeadlines);
              })
              this.tablesList = ret;
            } else {
              this.tablesList = []
            }
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    analysisImages(json) {
      try {
        json = JSON.parse(json);
        return this.fileBaseUrl + json.fileName;
      } catch (error) {
        return "";
      }
    }
  }
};
</script>

<style scoped lang="less">
.news {
  width: calc(100% );
  padding: 10px 30px;
  margin: 0 auto;
  background: #fff;
    .iheader {
      width: 95%;
      margin: 20px auto;
      display: flex;
      .box {
        display: flex;
        flex-direction: row;
        align-items: center;
        margin-left: 15px;
        .btn {
          width: 100px;
          height: 30px;
          text-align: center;
          border: none;
          outline: none;
          background: #5362e1;
          color: #fff;
          border-radius: 4px;
          margin-left: 10px;
        }
      }
    }
  .main {
    width: 95%;
    margin: 0px auto;
  }
  .page{
      text-align: center;
      margin: 20px 0;
    }
}
</style>